package com.zcx.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.zcx.R;
import com.zcx.base.BaseActivity;
import com.zcx.bean.PayBean;
import com.zcx.bean.PayResult;
import com.zcx.bean.WxPayBean;
import com.zcx.common.DialogHelper;
import com.zcx.common.Helper;
import com.zcx.common.ProgressDialog;
import com.zcx.presenter.PayPresenter;
import com.zcx.view.AlipayView;
import com.zcx.wxapi.WXPayEntryActivity;

import java.util.Map;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends BaseActivity<AlipayView,PayPresenter> implements AlipayView {

    private ProgressDialog progressDialog;
    private Map<String, String> result;
    private static final String ALI_PAY_SUCCESS = "9000";

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_main);
        findViewById(R.id.TextView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBasePresenter().alipay();
            }
        });
        findViewById(R.id.jumpWx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,WXPayEntryActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void startPay(int resId) {
        progressDialog = DialogHelper.showDialog(this, progressDialog, resId);
    }

    @Override
    public void getPaySucc(PayBean payBean) {
        DialogHelper.dismissDialog(progressDialog);
        if (Helper.isNull(payBean)) {
            return;
        }
        // 开始支付
        startAliPay(payBean);
    }

    private void startAliPay(final PayBean payBean) {
        //利用RxJava然唤起支付宝的支付
        Observable.create(new ObservableOnSubscribe<PayBean>() {
            @Override
            public void subscribe(ObservableEmitter<PayBean> e) throws Exception {
                PayTask alipay = new PayTask(MainActivity.this);
                result = alipay.payV2(payBean.getBody(),true);
                e.onComplete();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<PayBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PayBean payBean) {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if (Helper.isNotEmpty(result)) {
                            PayResult payResult = new PayResult(result);                         //照抄支付宝的demo就好了
                            if (TextUtils.equals(payResult.getResultStatus(), ALI_PAY_SUCCESS)) {
                                Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            } else {
                                getPayFail("支付取消");
                            }
                        } else {
                            getPayFail("支付取消");
                        }
                    }
                });
    }

    @Override
    public void getPayFail(String message) {
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getWxPaySucc(WxPayBean payBean) {

    }

    @Override
    protected PayPresenter setPresenter() {
        return new PayPresenter();
    }
}

